package com.yami.shop.app.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.zxing.WriterException;
import com.yami.shop.app.utils.QRCodeUtil;
import com.yami.shop.app.utils.ResultCodeEnum;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.param.ProductParam;
import com.yami.shop.common.util.Arith;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.yami.shop.app.utils.HttpResult;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/app/appProduct")
public class AppProductController {

    @Autowired
    private ProductService productService;

    /**
     * 查询店铺商品信息
     * */
    @PostMapping("/getProducts")
    @ResponseBody
    public HttpResult getProducts(@RequestBody ProductParam product, PageParam<Product> page){
        if(StringUtils.isNotEmpty(product.getProdName())){
            Pattern p_check = Pattern.compile("[\\u4e00-\\u9fa5]+");
            Matcher m = p_check.matcher(product.getProdName());
            if(m.find()&&m.group(0).equals(product.getProdName())){
                //全是拼音
                System.out.println(1);
            }
            else{
                System.out.println(2);
                product.setProdNamePy(product.getProdName().toUpperCase());
                product.setProdName(null);
            }
        }
        IPage<Product> products = productService.page(page,
                new LambdaQueryWrapper<Product>()
                        .like(product.getProdName()!=null, Product::getProdName, product.getProdName())
                        .like(product.getProdNamePy()!=null,Product::getProdNamePy,product.getProdNamePy())
                        .eq(Product::getShopId, SecurityUtils.getSysUser().getShopId())
                        .eq(product.getStatus() != null, Product::getStatus, product.getStatus())
                        .orderByDesc(Product::getSoldNum));
        List<Product> productList = products.getRecords();
        if(CollectionUtils.isNotEmpty(productList)){
            for(Product product1 : productList){
                    if(product1.getVipPrice()==null&&product1.getVipDiscount()!=0){
                        Double vipPrice = Arith.mul(product1.getPrice(),product1.getVipDiscount()/100);
                        product1.setVipPrice(new BigDecimal(vipPrice).setScale(2,BigDecimal.ROUND_HALF_UP));
                    }else{
                        product1.setVipPrice(new BigDecimal(product1.getPrice()));
                    }
            }
        }
       /*AppProductPool productParam = new AppProductPool();
        productParam.setShopId(shopId);
        productParam.setSaleType(2);//线下销售
        List<AppProductPool> productList = productService.getProductList(productParam);*/
        return HttpResult.success(products);
        //return HttpResult.failure("异常测试");
    }


    /**
     * 查询商品详情接口
     * */
    @PostMapping("getProductById")
    @ResponseBody
    public HttpResult getProductById(Long prodId){
        Product productParam = new Product();
        productParam.setShopId(SecurityUtils.getSysUser().getShopId());
        productParam.setProdId(prodId);
        List<Product> productList = productService.getProductList(productParam);
        return HttpResult.success(productList);
    }

    /**
     * 添加商品
     **/
    @PostMapping("/addProduct")
    @ResponseBody
    public HttpResult addProduct(@RequestBody ProductParam productParam){
        Product product = productService.getOne(
                new LambdaQueryWrapper<Product>()
                .eq(Product::getShopId,SecurityUtils.getSysUser().getShopId())
                .eq(Product::getBarCode,productParam.getBarCode())
        );
        if(product!=null){
            return HttpResult.failure("商品码已存在，请重新生成");
        }
        System.out.println("categoryList  "+productParam.getCategoryList());
        Long shopId=SecurityUtils.getSysUser().getShopId();
        Product productAdd = productService.insertProduct(productParam,shopId);
        return HttpResult.success(productAdd);
    }

    /**
     * 生成二维码的方法
     */
    @PostMapping("/produceQRCode")
    @ResponseBody
    public HttpResult produceQRCode(){
        String QrCode=null;
        StringBuffer buffer = new StringBuffer("https://upload.51xeld.com?cid=111");
        try {
             QrCode = QRCodeUtil.createQrCode(buffer.toString(),900,"JEPG");
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return HttpResult.success(QrCode);
    }

}
